#include "logindlg.h"
#include "ui_logindlg.h"
#include "mongo/userdata.h"
#include <QDateTime>
#include <QDebug>

LoginDlg::LoginDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDlg)
{
    ui->setupUi(this);
}

LoginDlg::~LoginDlg()
{
    delete ui;
}

void LoginDlg::on_pB_login_clicked()
{
    std::string account = ui->lE_account->text().toStdString();
    
    if(account==""||!USERDATA_MOGO->findAccount(account)){
        ui->label_msg->setText(tr("Account does not exist"));
        return;
    }
    
    std::string password = ui->lE_password->text().toStdString();
    if (password != USERDATA_MOGO->getValue(ACCOUNT,account,USERDATA_MOGO->UserKeyLists[PASSWORD])){
        ui->label_msg->setText(tr("Password error"));
        return;
    }
    
    USERDATA_MOGO->updateSingleValue(ACCOUNT,account,Last_Login_Time,QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss").toStdString());
    
    ui->label_msg->setText("");
    emit updatePermissions(ui->lE_account->text());
    accept();
}
